package com.jy.adscm.operation.hscrb.controller;


import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.hscrb.bean.MjglMould;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2018-12-18
 */
@RestController
@RequestMapping("/adscm/operation/mjglMould")
public class MjglMouldController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    com.jy.adscm.operation.hscrb.service.MjglMouldService MjglMouldService;
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody MjglMould mjglMould, HttpServletRequest request) throws AdscmSystemException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        mjglMould.setTaskcode(  Long.valueOf(new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date())) );
        mjglMould.setTaskreleaseperson(token.getUser().getId().toString());
        mjglMould.setTaskreleaseaddtime(new Date());
        mjglMould.setTaskfinishstate(0);
        mjglMould.setTaskstate(0);
        mjglMould.setIstaskstart("0");
        mjglMould.setIstaskbegin("0");
        mjglMould.setIstaskend("0");
        mjglMould.setIsreviewtask("0");
        mjglMould.setReviewresult("-1");
        boolean insert = MjglMouldService.insert(mjglMould);
        return new SimpleCoreResponse("200","新增成功",insert);
    }

    @RequestMapping("delete/{id}")
    public SimpleCoreResponse delete(@PathVariable Integer id)throws AdscmSystemException {
        boolean result = MjglMouldService.deleteById(id);
        return new  SimpleCoreResponse("200","删除成功",result);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody MjglMould mjglMould, HttpServletRequest request) throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        Boolean result = MjglMouldService.updateMjglMould(mjglMould,token);
        return  new SimpleCoreResponse("200","修改成功",result);
    }
    @RequestMapping("selectMjglMould/{taskState}/{taskFinishState}/{isReviewTask}")
    public  SimpleCoreResponse  selectMjglMould(@PathVariable Integer taskState ,@PathVariable Integer taskFinishState,@PathVariable Integer isReviewTask) throws  AdscmSystemException{
        List<MjglMould> mjglMoulds = MjglMouldService.selectMjglMould(taskState,taskFinishState,isReviewTask);
        return  new SimpleCoreResponse("200","查询成功",mjglMoulds);
    }

    @RequestMapping("selectTaskPerformPerson")
    public  SimpleCoreResponse  selectTaskPerformPerson() throws  AdscmSystemException{
        List<JyUser> users = MjglMouldService.selectTaskPerformPerson();
        return  new SimpleCoreResponse("200","查询成功",users);
    }
}

